package js.web.center.database;

import java.net.SocketTimeoutException;

import org.apache.log4j.Logger;

import js.web.center.utils.JsWebSyncCtrlUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;
/**
 * redis工具
 * @author dowell
 *
 */
public class JsWebRedis {
	private Logger logger=Logger.getLogger(JsWebRedis.class);
	private static JedisPool jedisPool=null;
	private static JsWebRedis factory;
	private String password;
	private int waitTime;
	private int idle;
	private int maxTotal;
	private String url;
	private int port;
	private int timeout;
	private static boolean islock=false;
	public static JsWebRedis getInstance(){
		if(factory==null){
			factory=new JsWebRedis();
		}
		return factory;
	}
	public synchronized Jedis getJedis(){
		int runOutTime=0;
		while(true){
			Jedis jedis = null;
			try{
				jedis=jedisPool.getResource();
				return jedis;
			}catch(Exception e){
				runOutTime++;
				if(e instanceof JedisConnectionException || e instanceof SocketTimeoutException){
					if(runOutTime>3){
						if(!islock){
							JsWebSyncCtrlUtils.executor.execute(new Runnable() {
								@Override
								public void run() {
									JsWebRedis.islock=true;
									while(JsWebRedis.islock){
										logger.info("正在重新初始化连接");
										try{
											Jedis resource = null;
											try{
												resource=jedisPool.getResource();
											}catch(Exception e){
												logger.error(e);
											}
											if(resource==null){
												init(waitTime, idle, maxTotal, url, port, timeout, password);
											}else{
												JsWebRedis.islock=false;
												resource.close();
												break;
											}
										}catch(Exception e){
											logger.error(e);
										}
									}
								}
							});
						}
						break;
					}else{
						logger.error("多次获取失败:"+runOutTime+":{}",e);
					}
				}else{
					break;
				}
			}
		}
		return null;
	}
	/**
	 * 初始化工具
	 * @param waitTime
	 * @param idle
	 * @param maxTotal
	 * @param url
	 * @param port
	 * @param timeout
	 * @param password
	 */
	public synchronized void init(int waitTime,int idle,int maxTotal,String url,int port,int timeout,String password){
		if(jedisPool!=null){
			try{
				jedisPool.close();
				jedisPool=null;
			}catch(Exception e){
				logger.error("JsWebRedis ERROR:{}",e);
			}
		}
		logger.info("正在初始化redis信息URL:"+url+" PORT:"+port+" PWD:"+password);
		this.waitTime=waitTime;
		this.idle=idle;
		this.maxTotal=maxTotal;
		this.url=url;
		this.port=port;
		this.timeout=timeout;
		this.password=password;
		this.password=password;
		JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
		jedisPoolConfig.setTestOnBorrow(false);
		jedisPoolConfig.setTestOnReturn(false);
		jedisPoolConfig.setMaxWaitMillis(waitTime);
		jedisPoolConfig.setMaxIdle(idle);
		jedisPoolConfig.setMaxTotal(maxTotal);
		jedisPool=new JedisPool(jedisPoolConfig, url, port, timeout, password);
	}
}
